package com.ihomey.linkuphome.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.ihomey.cc.library.util.DensityUtil;
import com.ihomey.linkuphome.DeviceController;
import com.ihomey.linkuphome.R;
import com.ihomey.linkuphome.adapter.GroupListAdapter;
import com.ihomey.linkuphome.base.BaseFragment;
import com.ihomey.linkuphome.entities.Device;
import com.ihomey.linkuphome.entities.GroupDevice;
import com.ihomey.linkuphome.util.BindView;
import com.ihomey.linkuphome.widget.swipemenulistview.SwipeMenu;
import com.ihomey.linkuphome.widget.swipemenulistview.SwipeMenuCreator;
import com.ihomey.linkuphome.widget.swipemenulistview.SwipeMenuItem;
import com.ihomey.linkuphome.widget.swipemenulistview.SwipeMenuListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/20.
 */

public class GroupFragment extends BaseFragment implements AdapterView.OnItemClickListener, SwipeMenuListView.OnMenuItemClickListener, GroupListAdapter.OnGroupSettingListener {

    @BindView(id = R.id.lv_group)
    private SwipeMenuListView lv_group;
    private GroupListAdapter groupAdapter;
    private DeviceController mController;
    private List<Device> groupList = new ArrayList<Device>();
    private SwipeMenuCreator swipeMenuCreator;

    private boolean isShare = false;

    public static GroupFragment newInstance() {
        GroupFragment fragment = new GroupFragment();
        return fragment;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        return inflater.inflate(R.layout.fragment_group, null);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mController = (DeviceController) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement DeviceController callback interface.");
        }
    }

    @Override
    public void initWidget(View parentView) {
        super.initWidget(parentView);
       swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                if (menu.getViewType() == 0) {
                    SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity().getApplicationContext());
                    deleteItem.setBackground(R.color.red);
                    deleteItem.setWidth(DensityUtil.dip2px(getActivity(), 64));
                    deleteItem.setTitle(R.string.delete);
                    deleteItem.setTitleSize(16);
                    deleteItem.setTitleColor(Color.WHITE);
                    menu.addMenuItem(deleteItem);
                }
            }
        };
        lv_group.setOnItemClickListener(this);
        lv_group.setOnMenuItemClickListener(this);
        loadData();
    }

    public void loadData(){
        isShare = getActivity().getSharedPreferences("ihomey", Activity.MODE_PRIVATE).getBoolean("isShare" + mController.getDeviceType(), false);
        groupList = mController.getGroups();
        groupAdapter = new GroupListAdapter(getActivity(), groupList, isShare);
        groupAdapter.setOnGroupSettingListener(this);
        lv_group.setAdapter(groupAdapter);
        if (!isShare) {
            lv_group.setMenuCreator(swipeMenuCreator);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        if (!isShare && position == groupAdapter.getCount() - 1) {
            GroupDevice newGroup = mController.addLightGroup();
            groupAdapter.addElem(newGroup);
        } else {
            GroupDevice groupDevice = (GroupDevice) groupAdapter.getItem(position);
            GroupParentFragment groupParentFragment = (GroupParentFragment) getParentFragment();
            groupParentFragment.updateControls(groupDevice.getDeviceId());
        }
    }

    @Override
    public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
        switch (index) {
            case 0:
                Device groupDevice = (Device) groupAdapter.getItem(position);
                mController.deleteLightGroup(groupDevice.getDeviceId());
                groupAdapter.remove(position);
                break;
        }
        return false;
    }

    @Override
    public void onGroupSettingClick(int position) {
        Device lawnGroup = (Device) groupAdapter.getItem(position);
        ((GroupParentFragment) getParentFragment()).showFragment(GroupSettingFragment.newInstance(lawnGroup.getDeviceId()));
    }

}
